package com.zxsc.zxmusic;

import java.util.ArrayList;
import java.util.List;

import com.zxsc.zxmusic.R;
import com.zxsc.zxmusic.fragment.BaseFragment;
import com.zxsc.zxmusic.fragment.LoginFragment;
import com.zxsc.zxmusic.fragment.RegisterFragment;
import com.zxsc.zxmusic.manager.ThemeManager;
import com.zxsc.zxmusic.manager.ThemeManager.IThemeListener;
import com.zxsc.zxmusic.widget.TitleBar;
import com.zxsc.zxmusic.widget.TitleBar.ITitleBackListener;

import android.R.color;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class UserActivity extends BaseActivity implements OnClickListener,
		OnPageChangeListener ,ITitleBackListener,IThemeListener{

	private TitleBar titleBar;
	private ViewPager viewPage;
	private LinearLayout linReg,linLogin;
	private TextView txtReg,txtLogin;
	private View viewReg,viewLogin;

	private BaseFragment fragmentLogin, fragmentRegister;

	private List<BaseFragment> fragments;
	private Adapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_user);		
		ThemeManager.with(this).registerListener(this );
		initView();
	}

	@SuppressWarnings("deprecation")
	private void initView() {

		titleBar = (TitleBar) findViewById(R.id.title);
		titleBar.setTitle("注册/登陆");
		titleBar.setOnBackListener(this);

		viewPage = (ViewPager) findViewById(R.id.user_viewpage);
		linLogin=(LinearLayout) findViewById(R.id.user_lin_login);
		linReg=(LinearLayout) findViewById(R.id.user_lin_register);

		linReg.setOnClickListener(this);
		linLogin.setOnClickListener(this);

		txtLogin=(TextView) findViewById(R.id.user_txt_login);
		txtReg=(TextView) findViewById(R.id.user_txt_register);

		viewLogin=(findViewById(R.id.user_view_login));
		viewReg=(findViewById(R.id.user_view_register));

		fragmentLogin = new LoginFragment();
		fragmentRegister = new RegisterFragment();

		fragments = new ArrayList<BaseFragment>();
		fragments.add(0, fragmentRegister);
		fragments.add(1, fragmentLogin);
		adapter = new Adapter(getSupportFragmentManager());
		viewPage.setAdapter(adapter);
		viewPage.setOnPageChangeListener(this);
		viewPage.setCurrentItem(0);
		setSel(0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_lin_login:
			setSel(1);
			viewPage.setCurrentItem(1);
			break;
		case R.id.user_lin_register:
			setSel(0);
			viewPage.setCurrentItem(0);
			break;

		default:
			break;
		}
	}

	class Adapter extends FragmentPagerAdapter {

		public Adapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragments.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragments.size();
		}

	}

	@Override
	public void onPageScrollStateChanged(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		setSel(arg0);

	}

	private void setSel(int position) {

		switch (position) {
		case 0:
			txtReg.setTextColor(ThemeManager.with(this).getCurrentColor());
			viewReg.setBackgroundColor(ThemeManager.with(this).getCurrentColor());

			txtLogin.setTextColor(Color.parseColor("#696969"));
			viewLogin.setBackgroundColor(Color.parseColor("#ffffff"));
			break;

		case 1:
			txtLogin.setTextColor(ThemeManager.with(this).getCurrentColor());
			viewLogin.setBackgroundColor(ThemeManager.with(this).getCurrentColor());
			
			txtReg.setTextColor(Color.parseColor("#696969"));
			viewReg.setBackgroundColor(Color.parseColor("#ffffff"));
			break;
		}
		

	}
	
	private void setSel(int position,int color) {

		switch (position) {
		case 0:
			txtReg.setTextColor(color);
			viewReg.setBackgroundColor(color);

			txtLogin.setTextColor(Color.parseColor("#696969"));
			viewLogin.setBackgroundColor(Color.parseColor("#ffffff"));
			break;

		case 1:
			txtLogin.setTextColor(color);
			viewLogin.setBackgroundColor(color);
			
			txtReg.setTextColor(Color.parseColor("#696969"));
			viewReg.setBackgroundColor(Color.parseColor("#ffffff"));
			break;
		}
		

	}
	

	@Override
	public void onTitleBackClick() {
		onBackPressed();
	}

	@Override
	public void onThemeChange(int color) {
		// TODO Auto-generated method stub
		setSel(viewPage.getCurrentItem(),color);
	}

	

}
